class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        int n = nums.size();
        sort(nums.begin(), nums.end());
        for (int i = 0; i < n;)
        {
            int left = i + 1, right = nums.size() - 1, aim = -nums[i];
            if (aim < 0) break;
            while (left < right)
            {
                int sum = nums[left] + nums[right];
                if (sum > aim) right--;
                else if (sum < aim) left++;
                else {
                    ret.push_back({ nums[i],nums[left++],nums[right--] });
                    while (left < right && nums[left - 1] == nums[left]) left++;
                    while (left < right && nums[right] == nums[right + 1]) right--;
                }

            }
            i++;
            while (i < n && nums[i - 1] == nums[i]) i++;
        }
        return ret;
    }
};
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> ret;
        int n = nums.size();
        sort(nums.begin(), nums.end());
        for (int i = 0; i < n;)
        {
            for (int j = i + 1; j < n;)
            {

                int left = j + 1, right = nums.size() - 1;
                long long aim = (long long)target - nums[i] - nums[j];
                while (left < right)
                {
                    int sum = nums[left] + nums[right];
                    if (sum > aim) right--;
                    else if (sum < aim) left++;
                    else
                    {
                        ret.push_back({ nums[i],nums[left++],nums[right--],nums[j] });
                        while (left < right && nums[left - 1] == nums[left]) left++;
                        while (left < right && nums[right] == nums[right + 1]) right--;
                    }

                }
                j++;
                while (j < n && nums[j - 1] == nums[j]) j++;
            }
            i++;
            while (i < n && nums[i - 1] == nums[i])i++;
        }
        return ret;

    }
};
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        int sum = 0, len = INT_MAX;
        int left = 0, right = 0;
        while (right < n)
        {
            sum += nums[right];
            while (sum >= target)
            {
                len = min(len, right - left + 1);
                sum -= nums[left++];
            }
            right++;
        }
        return len == INT_MAX ? 0 : len;

    }
};
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int n = s.size();
        int hash[128] = { 0 };
        int left = 0, right = 0, ret = 0;
        while (right < n)
        {
            hash[s[right]]++;
            while (hash[s[right]] > 1)
                hash[s[left++]]--;
            ret = max(ret, right - left + 1);
            right++;
        }
        return ret;
    }
};